package com.cskaoyan.service.impl;

import com.cskaoyan.bean.data.CatalogyData;
import com.cskaoyan.bean.data.CatalogyListData;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.model.MarketCategory;
import com.cskaoyan.model.MarketCategoryExample;
import com.cskaoyan.service.CategoryService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    MarketCategoryMapper categoryMapper;


    @Override
    public List queryLevelOne() {
        MarketCategoryExample example = new MarketCategoryExample();
        example.createCriteria().andDeletedEqualTo(false)
                .andLevelEqualTo("L1");
        example.setOrderByClause("add_time desc");

        List<MarketCategory> marketCategories = categoryMapper.selectByExample(example);
        /* 方式一：遍历再使用map存储channel中的单条数据
        List<Map> list = new ArrayList<>();
        for (MarketCategory marketCategory : marketCategories) {
            HashMap<Object, Object> map = new HashMap<>();
            map.put("iconUrl", marketCategory.getIconUrl());
            map.put("id", marketCategory.getId());
            map.put("name", marketCategory.getName());
            list.add(map);
        }
        return list;
        */

        //方式二：使用流，再使用map来存储单条数据（建议）
        List<Map<Object, Object>> list = marketCategories.stream().map(marketCategory -> {
            Map<Object, Object> map = new HashMap<>();
            map.put("iconUrl", marketCategory.getIconUrl());
            map.put("id", marketCategory.getId());
            map.put("name", marketCategory.getName());
            return map;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<MarketCategory> list(String level, int page, int limit) {
        MarketCategoryExample example = new MarketCategoryExample();
        example.createCriteria().andLevelEqualTo(level)
                .andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc");
        PageHelper.startPage(page, limit);
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(example);
        return marketCategories;
    }

    @Override
    public List<MarketCategory> queryChildren(Integer categoryId) {
        MarketCategoryExample example = new MarketCategoryExample();
        example.setOrderByClause("add_time desc");
        example.createCriteria().andDeletedEqualTo(false)
                .andPidEqualTo(categoryId);
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(example);

        return marketCategories;
    }

    @Override
    public MarketCategory findById(Integer id) {
        MarketCategory category = categoryMapper.selectByPrimaryKey(id);
        return category;
    }

    @Override
    public CatalogyData queryCurrentById(int id) {

        MarketCategory currentCategory = findById(id);
        List<MarketCategory> currentSubCategory = queryChildren(id);

        CatalogyData data = new CatalogyData();
        data.setCurrentCategory(currentCategory);
        data.setCurrentSubCategory(currentSubCategory);
        return data;
    }

    @Override
    public CatalogyListData querCurrentList() {
        // L1的列表
        MarketCategoryExample example = new MarketCategoryExample();
        example.createCriteria().andLevelEqualTo("L1")
                .andDeletedEqualTo(false);
        List<MarketCategory> categoryList = categoryMapper.selectByExample(example);
        //第一种类型的首个详细内容
        MarketCategory currentCategory = categoryList.get(0);
        // 子类
        MarketCategoryExample example1 = new MarketCategoryExample();
        example1.createCriteria().andPidEqualTo(currentCategory.getId())
                .andDeletedEqualTo(false);
        List<MarketCategory> currentSubCategory = categoryMapper.selectByExample(example1);

        CatalogyListData data = new CatalogyListData();
        data.setCategoryList(categoryList);
        data.setCurrentCategory(currentCategory);
        data.setCurrentSubCategory(currentSubCategory);
        return data;
    }

    @Override
    public MarketCategory findCurrentCategory(Integer id) {
        //1. 先判断一下id的level是否为L1，是的话查询子类目，并返回子类目列表的第一个
        //2. 如果不是的话，查询的id就是二级类目的信息
        MarketCategory marketCategory = this.findById(id);
        if ("L1".equals(marketCategory.getLevel())) {
            List<MarketCategory> marketCategories = this.queryChildren(id);
            return marketCategories.get(0);
        } else {
            return this.findById(id);
        }
    }

    //根据二级类名的id查询对应的父类商品
    @Override
    public MarketCategory findFatherById(Integer categoryId) {
        //1. 先根据二级类目id查询对应的商品
        MarketCategory marketCategory = categoryMapper.selectByPrimaryKey(categoryId);

        //2. 查询该商品对应的一级类目商品
        //2.1 构造查询条件
        MarketCategoryExample example = new MarketCategoryExample();
        example.createCriteria().andDeletedEqualTo(false)
                .andIdEqualTo(marketCategory.getPid());

        //2.3 进行查询
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(example);

        return marketCategories != null && marketCategories.size() == 1 ? marketCategories.get(0) : null;
    }

    @Override
    public List<MarketCategory> queryBrother(Integer id) {
        //1. 根据类名id查询类目信息
        MarketCategory category = this.findById(id);
        //1.1 存储商品二级类名分类的信息
        List<MarketCategory> marketCategories = null;

        //2. 对类名信息进行判断
        if ("L1".equals(category.getLevel())) {
            marketCategories = this.queryChildren(id);
        }
        if ("L2".equals(category.getLevel())) {
            MarketCategory marketCategory = this.findFatherById(id);
            marketCategories = this.queryChildren(marketCategory.getId());
        }
        return marketCategories;
    }
}
